<?php

namespace App\Http\Controllers\Api\V5;

use App\Models\V1\Comment;
use App\Models\V1\Lessons;
use App\Models\V1\LessonsClassRoom;
use App\Module\EntMemberInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\ApiData;
use DB;

class LessonsClassRoomController extends BaseController
{
    protected $apiData;

    public function __construct(ApiData $apiData)
    {
        $this->apiData = $apiData;
    }

    /**
     * 获取所有的教室的课程信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        //获取所有的课程信息
        $collect = LessonsClassRoom::with('hasManyLessons')->get();

        //剔除课程房间的数据中的重复的图片
        $data = $collect->map(function ($items) {
            $data = collect($items)->forget('default_img')->map(function ($item) {
                if (is_array($item)) {
                    return array_slice($item, 0, 4);
                } else {
                    return $item;
                }
            });
            return $data;
        });


        //拿出课程房间中的数据重复的图片
        $default_img = $collect->map(function ($items) {
            return collect($items)->only('default_img');
        })->toArray();

        return $this->apiData->set_data('lessons_category', $data)
            ->set_data('default_img', $default_img[0]['default_img'])
            ->send(200, '获取信息成功');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @internal param 根据课程的分类的ID获取具体的一个分类下面的所有的课程 $id
     */
    public function lessons(Request $request)
    {

        try {
            $id = $request->id;
            $data = LessonsClassRoom::findOrFail($id)->hasManyLessons()->paginate(2)->toArray();
            return $this->apiData->set_data('lessons_all', $data)
                ->send(200, '获取信息成功');
        } catch (\Exception $exception) {
            return $this->apiData->send(204, '数据没有发现');
        }
    }

    /**
     * @param Request $request
     * @param Lessons $lessons
     * @return \Illuminate\Http\JsonResponse
     * @internal param 根据某一个课程获取详细信息 $lessonId
     * @internal param 推荐课程的长度 $length
     */
    public function lesson(Request $request, Lessons $lessons)
    {
        $lessonId = $request->id;

        $userPhone = $request->userPhone;//获取当前用户的手机号

        $lessons_data = $lessons->get_lessons_data($lessonId, 2, $userPhone);

        return $this->apiData->set_data('lessons_detail', $lessons_data)
            ->send(200, '获取信息成功');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @internal param 某一个评论的ID $comment_id
     */
    public function delete_comment(Request $request)
    {
        $comment_id = $request->commentId;
        $status = Comment::find($comment_id);
        if ($status) {
            $status = Comment::destroy($comment_id);
            return $this->apiData->set_data('delete_data', '删除成功')->send(200, 'success');
        }
        return $this->apiData->set_data('delete_data', '删除失败')->send(400, 'delete error');
    }

}
































